package com.adminx.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.adminx.dao.ResourceDao;
import com.adminx.entity.Resource;
import com.adminx.pojo.NavigationMenuPojo;
import com.adminx.pojo.ResourcePojo;
import com.adminx.util.PageResults;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ResourceService {
	
	
	@Autowired
	private ResourceDao resourceDao;
	
	/**
	 * 查询资源列表
	 * @param pageIndex
	 * @param pageSize
	 * @param searchText
	 */
	public PageResults<ResourcePojo> query(Integer pageIndex, Integer pageSize, String searchText, Long parentId){
		return this.resourceDao.query(pageIndex, pageSize, searchText, parentId);
	}
	
	/**
	 * 保存或修改资源
	 * @param authority
	 */
	public void save(Resource resource) {
		resource.setParentID((Long) (null == resource.getParentID()?0:resource.getParentID()));
		this.resourceDao.saveOrUpdate(resource);
	}
	
	/**
	 * 修改资源
	 */
	public void update(Resource resource) {
		Resource oldResource = this.resourceDao.get(resource.getResourceId());
		oldResource.setResourcesName(resource.getResourcesName());
		oldResource.setResourceType(resource.getResourceType());
		oldResource.setResourcesPath(resource.getResourcesPath());
		oldResource.setRemark(resource.getRemark());
		oldResource.setResourcesIcon(resource.getResourcesIcon());
		this.resourceDao.update(oldResource);
	}
	
	/**
	 * 新增资源
	 */
	public void add(Resource resource){
		if(resource.getResourceId() == null){
			resource.setParentID(0L);
			this.resourceDao.save(resource);
		}else{
			resource.setParentID(resource.getResourceId());
			resource.setResourceId(null);
			this.resourceDao.save(resource);
		}
	}
	
	/**
	 * 删除资源
	 */
	public void delete(Long resourceId){
		this.resourceDao.deleteById(resourceId);
	}
	
	/**
	 * 删除资源
	 * @param roleID 角色ID
	 * @return 角色
	 */
	public int delBatch(String ids){
		return this.resourceDao.delBatch(ids);
	}
	
	/**
	 * 通过ID查询资源
	 * @param resourceId
	 * @return Resource
	 */
	public Resource info(Long resourceId){
		String hql = "From Resource t where t.resourceId = ?";
		return this.resourceDao.getByHQL(hql,resourceId);
	}
	
	/**
	 * 查询导航数据
	 * @return 
	 */
	public NavigationMenuPojo queryNavigation(String resourcePath){
		return this.resourceDao.queryNavigation(resourcePath);
	}
}
